<?php
namespace app\admin\model;

use think\Model;
use think\Db;

class DepartmentModel extends Model
{
    // 确定链接表名
    protected $name = 'department';

    /**
     * 获取部门数据
     */
    public function getDepartmentInfo($id)
    {
        $result = $this->field('id,name,pid')->select();
        $str = '';

        $role = new RoleModel();
        $rule = $role->getRuleById($id);

        if(!empty($rule)){
            $rule = explode(',', $rule);
        }

        foreach($result as $key=>$vo){
            $str .= '{ "id": "' . $vo['id'] . '", "pId":"' . $vo['pid'] . '", "name":"' . $vo['name'].'"';

            if(!empty($rule) && in_array($vo['id'], $rule)){
                $str .= ' ,"checked":1';
            }

            $str .= '},';

        }

        return '[' . rtrim($str, ',') . ']';
    }

    /**
     * 获取部门数据
     * @return mixed
     */
    public function getDepartmentList()
    {
        return $this->select();
    }

    /**
     * 插入部门信息
     * @param $param
     */
    public function insertDepart($param)
    {
        try{

            $this->save($param);
            return msg(1, '', '添加部门成功');
        }catch(PDOException $e){

            return msg(-2, '', $e->getMessage());
        }
    }

    /**
     * 编辑部门
     * @param $param
     */
    public function editDepart($param)
    {
        try{

            $this->save($param, ['id' => $param['id']]);
            return msg(1, '', '编辑部门成功');
        }catch(PDOException $e){

            return msg(-2, '', $e->getMessage());
        }
    }

    /**
     * 删除部门
     * @param $id
     */
    public function delDepart($id)
    {
        try{

            $this->where('id', $id)->delete();
            return msg(1, '', '删除部门成功');

        }catch(PDOException $e){
            return msg(-1, '', $e->getMessage());
        }
    }

    /**
     * [获取所有子部门ID]
     */
    public function getSonsByPid($id,$is_2d=true){
        $arr = [];
        $lower = Db::name('department')->where('pid',$id)->field('id')->select();
        foreach($lower as $k => $v){
            if($v){
                array_push($arr,$v);
                array_push($arr,$this->getSonsByPid($v['id']));
            }
        }
        if($is_2d){
            $arr = $this->ChangeArrayDimension($arr);
        }
        $arr = array_merge(array_filter($arr));
        return $arr;
    }

    // 三维数组转二维数组
    public function ChangeArrayDimension ($arr_3d){
        $arr_2d = [];
        foreach($arr_3d as $k => $v){
            foreach($arr_3d[$k] as $ke => $va){
                if(is_array($va)){
                    array_push($arr_2d,$va);
                }
            }
            if(!is_array($va)){
                array_push($arr_2d,$v);
            }
        }
        return $arr_2d;
    }

	/**
	 * [获取顶级部门by子部门ID]
	 */
	public function getTopDepartBySon($id){
		$pid = $this->where('id',$id)->value('pid');
		if($pid != 0){
			return $this->getTopDepartBySon($pid);
		}
		return $id;
	}

	/**
	 * [获取自上而下部门字符串]
	 */
	public function getTotalDepartStr($id=-1,$str=''){
		$pid = $this->where('id',$id)->value('pid');
		$name = $this->where('id',$id)->value('name');
		if($str != ''){
			$str = $name.'-'.$str;
		}else{
			$str = $name;
		}
		if ($pid != 0) {
			return $this->getTotalDepartStr($pid,$str);
		}
		return $str;
	}
}
